
The language construct empty() appears rather versatile. It’s like a Swiss army knife with a thousand blades, ready to hurt you if you grab it by the wrong end. Or a jack of all trades, master of none. Most of all, empty() is a poor communicator.
I spot empty() in poorly-aged closed-source projects. I discover empty() in brand-new empty() again in open-source projects with millions of downloads. So what is the problem with empty() when so many people use it?Problem
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals false. empty() does not generate a warning if the variable does not exist.
PHP manual: empty()
If you ignore the internal implementation and possible performance issues, using empty() is identical to using isset() and a Losse Comparision withfalse.
Candidates
Undefined variable
Scenario: Expecting an included file to declare a variable
You include a file that you expect to declare a variable. The file may not exist or may not declare the variable.
Instead of using empty() to verify that an included file exists and declares a variable, set the variable to a suitable default value, include the file when it exists, and verify that the variable has an acceptable type and value.
💡 Avoid writing code that relies on including files that declare variables or return values.
Undefined instance property
value)); // 👋(bool)true
Undefined static property
console.log( 'Code is Poetry' );
Inaccessible instance property
value)); // 👋(bool)true
var_dump(empty($object->otherValue)); // 👋(bool)true
Inaccessible static property
Scenario: Declaring variables in files and including them
Null
Scenario: Instance or static property could be null
value)) {
+ if ($this->value === null) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === null) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be null
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
Scenario: Function or method return value could be null
bar()) {
+if ($foo->bar() === null) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === null) {
// ...
}
💡 Avoid writing functions or methods that return values of multiple types. Add return type declarations or DocBlocks to document function and method return types.
Array
Scenario: Instance or static property could be an empty array
?php
declare(strict_types=1);
final class Foo
{
private $value;
public function bar()
{
- if (empty($this->value)) {
+ if ($this->value === []) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === []) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be an empty array
bar()) {
+if ($foo->bar() === []) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === []) {
// ...
}
Boolean
Scenario: Instance or static property could be false
You have a class with an instance or a static property and currently use empty() to verify the property value.
Instead of using empty() to verify the property value when the property can assume multiple types, compare the instance or static property with false and handle each possible case separately.
value)) {
+ if ($this->value === false) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if (!$this->value) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be false
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
Scenario: Function or method return value could be false
bar()) {
+if ($foo->bar() === false) {
// ...
}
+// handle other possible types and values
bar()) {
+if (!$foo->bar()) {
// ...
}
💡 Avoid writing functions or methods that return values of multiple types. Add return type declarations or DocBlocks to document function and method return types.
Float
Scenario: Instance or static property could be 0.0
value)) {
+ if ($this->value === 0.0) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === 0.0) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be 0.0
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
Scenario: Function or method return value could be 0.0
bar()) {
+if ($foo->bar() === 0.0) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === 0.0) {
// ...
}
Int
Scenario: Instance or static property could be 0
value)) {
+ if ($this->value === 0) {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === 0) {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be 0
You have a function or a method with a parameter that could be an int with the value 0.
Instead of using empty() to verify the parameter value when the parameter can assume multiple types, compare the parameter with 0 and handle each possible case separately.
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
Scenario: Function or method return value could be 0
bar()) {
+if ($foo->bar() === 0) {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === 0) {
// ...
}
String
Scenario: Instance or static property could be an empty string
You have a class with an instance or a static property and currently use empty() to verify the property value.
Instead of using empty() to verify the property value when the property can assume multiple types, compare the instance or static property with ‘ ‘ (or ‘0’) and handle each possible case separately.
value)) {
+ if ($this->value === '') {
// ...
}
+ // handle other possible types and values
+
// ...
}
}
value)) {
+ if ($this->value === '') {
// ...
}
// ...
}
}
💡 Avoid writing classes with instance or static properties that accept multiple types. Add property type declarations or DocBlocks to document property types.
Scenario: Function or method parameter could be an empty string
You have a function or a method with a parameter that could be a string with the value ” (or '0'
).
💡 Avoid writing functions or methods with parameters that accept multiple types. Add parameter type declarations or DocBlocks to document function and method parameter types.
Scenario: Function or method return value could be an empty string
You have a function or a method with a return value that could be a string with the value ” (or ‘0’).
Instead of using empty() to verify the return value at the call site when the return value can assume multiple types, compare the return value with ‘ ‘ (or ‘0’) and handle each possible case separately.
bar()) {
+if ($foo->bar() === '') {
// ...
}
+// handle other possible types and values
bar()) {
+if ($foo->bar() === '') {
// ...
}
SimpleXMLElement
');
var_dump(empty($value)); // (bool)true